//
//  UICollectionView+IndexPath.m
//  PinterestTest
//
//  Created by long on 14-10-16.
//  Copyright (c) 2014年 long. All rights reserved.
//

#import "UICollectionView+IndexPath.h"
#import <objc/runtime.h>

static NSString * const KIndexPathKey = @"kIndexPathKey";

@implementation UICollectionView (IndexPath)

-(void)setCurrentIndexPath:(NSIndexPath *)indexPath
{
    //通过此函数保存indexPath
    objc_setAssociatedObject(self, &KIndexPathKey, indexPath, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSIndexPath *)currentIndexPath
{
    NSIndexPath * indexPath = objc_getAssociatedObject(self, &KIndexPathKey);
    return indexPath;
}

/*
 添加下拉刷新
 
 @param refreshingBlock 下拉刷新回调
 */
-(void)addHeadRefreshWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
{
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (refreshingBlock) {
            refreshingBlock();
        }
    }];
}

/*
 停止下拉刷新
 */
-(void)stopHeadRefresh
{
    [self.mj_header endRefreshing];
}

/*
 停止下拉加载更多
 */
-(void)stopFooterRefresh
{
    [self.mj_footer endRefreshing];
}

/**
 添加上拉加载更多
 
 @param refreshingBlock 下拉刷新回调
 */
-(void)addFooterRefreshWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock
{
    self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (refreshingBlock) {
            refreshingBlock();
        }
    }];
}

-(void)endRefresh{
    if (self.mj_header.isRefreshing) {
        self.mj_header.hidden = NO;
        [self.mj_header endRefreshing];
    }
    if (self.mj_footer.isRefreshing) {
        self.mj_footer.hidden = NO;
        [self.mj_footer endRefreshing];
    }
}

@end
